package com.dgut.demo.controller;

import com.dgut.demo.bean.HandlersMessage;
import com.dgut.demo.bean.ReturnMessage;
import com.dgut.demo.bean.User;
import com.dgut.demo.service.HandlersService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther 虎
 * @Data 2021/2/16 16:01
 * @Version 1.0
 */
@RestController
public class HandlersController {
    @Autowired
    private HandlersService handlersService;
    @Autowired
    private LogsController logsController;
    @Autowired
    private User user;

    @PutMapping("/updateHandlerPassword")
    public int updateHandlerPassword(String heid, String passwords1, String passwords){
        return handlersService.updateHandlerPassword(heid,passwords1,passwords);
    }

    @GetMapping("/getOneHandlersMessage")
    public ReturnMessage getOneHandlersMessage(String username,String password){
        try {
            HandlersMessage handlersMessage = handlersService.getOneHandlersMessage(username);
            ReturnMessage returnMessage = new ReturnMessage();
            if(handlersMessage !=null){
                if(handlersMessage.getPasswords().equals(password)){
                    user.setUserId(handlersMessage.getHeid());
                    returnMessage.setCode(200);
                    returnMessage.setMessage(handlersMessage.getHeid());
                    //log
                    String actions = "成功登陆系统！";
                    String id = handlersMessage.getHeid();
                    logsController.addOneLogsMessage(actions,id);

                }else{
                    returnMessage.setCode(401);
                    returnMessage.setMessage("密码错误！");
                }
            }else {
                returnMessage.setCode(404);
                returnMessage.setMessage("账号不存在！");
            }
            return returnMessage;
        }catch (Exception exception){
            ReturnMessage returnMessage = new ReturnMessage();
            returnMessage.setCode(500);
            returnMessage.setMessage("服务器出错！");
            return returnMessage;
        }
    }

    @PostMapping("/exit")
    public ReturnMessage exit(String userId){
        ReturnMessage returnMessage = new ReturnMessage();
        returnMessage.setCode(200);
        returnMessage.setMessage("退出成功！");
        //log
        String actions = "退出系统！";
        logsController.addOneLogsMessage(actions,userId);
        return returnMessage;
    }
}
